package com.xiangxiao.rpan.storage.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.xiangxiao.rpan.storage.constant.GlobalConsts;
import org.apache.commons.lang3.StringUtils;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/12/5 11:32
 */
public class StringUtil {

  private static URLUtil URLEncodeUtil;

  public static final char DELIMITER = '/';

  public static final String DELIMITER_STR = "/";

  public static final String HTTP_PROTOCOL = "http://";

  public static final String HTTPS_PROTOCOL = "https://";

  /**
   * 编码全部字符
   * @param   str
   *          被编码内容
   * @return  编码后的字符
   */
  public static String encodeAllIgnoreSlashes(String str) {
    if (StringUtils.isEmpty(str)) {
      return str;
    }

    StringBuilder sb = new StringBuilder();

    int prevIndex = -1;
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      if (c == GlobalConsts.PATH_SEPARATOR_CHAR) {
        if (prevIndex < i) {
          String substring = str.substring(prevIndex + 1, i);
          sb.append(URLEncodeUtil.encodeAll(substring));
          prevIndex = i;
        }
        sb.append(c);
      }

      if (i == str.length() - 1 && prevIndex < i) {
        String substring = str.substring(prevIndex + 1, i + 1);
        sb.append(URLEncodeUtil.encodeAll(substring));
      }
    }

    return sb.toString();
  }

  /**
   * 拼接 URL，并去除重复的分隔符 '/'，但不会影响 http:// 和 https:// 这种头部.
   * @param   strs
   *          拼接的字符数组
   * @return  拼接结果
   */
  public static String concat(String... strs) {
    StringBuilder sb = new StringBuilder(DELIMITER_STR);
    for (int i = 0; i < strs.length; i++) {
      String str = strs[i];
      if (StringUtils.isEmpty(str)) {
        continue;
      }
      sb.append(str);
      if (i != strs.length - 1) {
        sb.append(DELIMITER);
      }
    }
    return removeDuplicateSlashes(sb.toString());
  }

  /**
   * 移除 URL 中的第一个 '/'
   * @param   path
   *          路径
   * @return  如 path = '/folder1/file1', 返回 'folder1/file1'
   *          如 path = '/folder1/file1', 返回 'folder1/file1'
   */
  public static String trimStartSlashes(String path) {
    if (StrUtil.isEmpty(path)) {
      return path;
    }

    while (path.startsWith(DELIMITER_STR)) {
      path = path.substring(1);
    }
    return path;
  }

  /**
   * 去除路径中所有重复的 '/'
   * @param   path
   *          路径
   * @return  如 path = '/folder1//file1/', 返回 '/folder1/file1/'
   *          如 path = '/folder1////file1///', 返回 '/folder1/file1/'
   */
  public static String removeDuplicateSlashes(String path) {
    if (StringUtils.isEmpty(path)) {
      return path;
    }

    StringBuilder sb = new StringBuilder();

    // 是否包含 http 或 https 协议信息
    boolean containProtocol =  StrUtil.containsAnyIgnoreCase(path, HTTP_PROTOCOL, HTTPS_PROTOCOL);

    if (containProtocol) {
      path = trimStartSlashes(path);
    }

    // 是否包含 http 协议信息
    boolean startWithHttpProtocol = StrUtil.startWithIgnoreCase(path, HTTP_PROTOCOL);
    // 是否包含 https 协议信息
    boolean startWithHttpsProtocol = StrUtil.startWithIgnoreCase(path, HTTPS_PROTOCOL);

    if (startWithHttpProtocol) {
      sb.append(HTTP_PROTOCOL);
    } else if (startWithHttpsProtocol) {
      sb.append(HTTPS_PROTOCOL);
    }

    for (int i = sb.length(); i < path.length() - 1; i++) {
      char current = path.charAt(i);
      char next = path.charAt(i + 1);
      if (!(current == DELIMITER && next == DELIMITER)) {
        sb.append(current);
      }
    }
    sb.append(path.charAt(path.length() - 1));
    return sb.toString();
  }

  public static String removeAllLineBreaksAndTrim(String str) {
    String removeResult = StrUtil.removeAllLineBreaks(str);
    return StrUtil.trim(removeResult);
  }

  /**
   * 拼接 URL，并去除重复的分隔符 '/'，并去除开头的 '/', 但不会影响 http:// 和 https:// 这种头部.
   * @param   strs
   *          拼接的字符数组
   * @return  拼接结果
   */
  public static String concatTrimStartSlashes(String... strs) {
    return trimStartSlashes(concat(strs));
  }


  /**
   * 拼接 URL，并去除重复的分隔符 '/'，并去除结尾的 '/', 但不会影响 http:// 和 https:// 这种头部.
   * @param   strs
   *          拼接的字符数组
   * @return  拼接结果
   */
  public static String concatTrimEndSlashes(String... strs) {
    return trimEndSlashes(concat(strs));
  }

  /**
   * 移除 URL 中的最后一个 '/'
   * @param   path
   *          路径
   * @return  如 path = '/folder1/file1/', 返回 '/folder1/file1'
   *          如 path = '/folder1/file1///', 返回 '/folder1/file1'
   */
  public static String trimEndSlashes(String path) {
    if (StrUtil.isEmpty(path)) {
      return path;
    }

    while (path.endsWith(DELIMITER_STR)) {
      path = path.substring(0, path.length() - 1);
    }

    return path;
  }


  /**
   * 拼接 URL，并去除重复的分隔符 '/'，并去除开头和结尾的 '/', 但不会影响 http:// 和 https:// 这种头部.
   * @param   strs
   *          拼接的字符数组
   * @return  拼接结果
   */
  public static String concatTrimSlashes(String... strs) {
    return trimSlashes(concat(strs));
  }

  /**
   * 移除 URL 中的前后的所有 '/'
   * @param   path
   *          路径
   * @return  如 path = '/folder1/file1/', 返回 'folder1/file1'
   *          如 path = '///folder1/file1//', 返回 'folder1/file1'
   */
  public static String trimSlashes(String path) {
    path = trimStartSlashes(path);
    path = trimEndSlashes(path);
    return path;
  }


  public static String getParentPath(String path) {
    int toIndex = StrUtil.lastIndexOfIgnoreCase(path, GlobalConsts.PATH_SEPARATOR);
    if (toIndex <= 0) {
      return "/";
    } else {
      return StrUtil.sub(path, 0, toIndex);
    }
  }
}
